package com.zj.fx.service.info;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zj.fx.common.base.BaseService;
import com.zj.fx.common.support.ConvertUtil;
import com.zj.fx.mapper.auto.TsysUserMapper;
import com.zj.fx.mapper.info.InfoSubcompanyMapper;
import com.zj.fx.model.auto.TsysRole;
import com.zj.fx.model.auto.TsysRoleExample;
import com.zj.fx.model.auto.TsysUser;
import com.zj.fx.model.custom.InfoSubcompanyVo;
import com.zj.fx.model.custom.InfoSupplyVo;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.model.info.*;
import com.zj.fx.util.SnowflakeIdWorker;
import com.zj.fx.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 分公司信息表 InfoSubcompanyService
 *
 * @author SQL_自动生成
 * @Title: InfoSubcompanyService.java 
 * @Package com.zj.fx.service 
 * @email ${email}
 * @date 2023-07-05 10:48:53  
 **/
@Service
public class InfoSubcompanyService implements BaseService<InfoSubcompany, InfoSubcompanyExample> {
    @Autowired
    private InfoSubcompanyMapper infoSubcompanyMapper;
    @Autowired
    private TsysUserMapper tsysUserMapper;

    /**
     * 分页查询
     *
     * @param
     * @param
     * @return
     */
    public PageInfo<InfoSubcompany> list(Tablepar tablepar, InfoSubcompany infoSubcompany) {
        InfoSubcompanyExample testExample = new InfoSubcompanyExample();
        //搜索
        if (StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
            testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
        } else {//大搜索
            testExample.createCriteria().andLikeQuery(infoSubcompany);
        }
        //表格排序
        //if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
        //}else{
        //	testExample.setOrderByClause("id ASC");
        //}
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<InfoSubcompany> list = infoSubcompanyMapper.selectByExample(testExample);
        PageInfo<InfoSubcompany> pageInfo = new PageInfo<InfoSubcompany>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String ids) {

        List<String> lista = ConvertUtil.toListStrArray(ids);
        InfoSubcompanyExample example = new InfoSubcompanyExample();
        example.createCriteria().andIdIn(lista);
        return infoSubcompanyMapper.deleteByExample(example);


    }


    @Override
    public InfoSubcompany selectByPrimaryKey(String id) {

        return infoSubcompanyMapper.selectByPrimaryKey(id);

    }


    @Override
    public int updateByPrimaryKeySelective(InfoSubcompany record) {
        return infoSubcompanyMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 添加
     */
    @Override
    public int insertSelective(InfoSubcompany record) {
        //添加雪花主键id
        record.setId(record.getNum());
        record.setState(InfoConstant.STATE_ENABLE);
        record.setCreateTime(new Date());
        return infoSubcompanyMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(InfoSubcompany record, InfoSubcompanyExample example) {

        return infoSubcompanyMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(InfoSubcompany record, InfoSubcompanyExample example) {

        return infoSubcompanyMapper.updateByExample(record, example);
    }

    @Override
    public List<InfoSubcompany> selectByExample(InfoSubcompanyExample example) {

        return infoSubcompanyMapper.selectByExample(example);
    }


    @Override
    public long countByExample(InfoSubcompanyExample example) {

        return infoSubcompanyMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(InfoSubcompanyExample example) {

        return infoSubcompanyMapper.deleteByExample(example);
    }

    /**
     * 检查编号是否唯一
     * @param infoSubcompany
     * @return
     */
    public int checkNumUnique(InfoSubcompany infoSubcompany) {
        InfoSubcompanyExample infoSubcompanyExample = new InfoSubcompanyExample();
        infoSubcompanyExample.createCriteria().andNumEqualTo(infoSubcompany.getNum());
        List<InfoSubcompany> infoSubcompanies = infoSubcompanyMapper.selectByExample(infoSubcompanyExample);
        return infoSubcompanies.size();

    }

    /**
     * 查询List
     * @return
     */
    public List<InfoSubcompany> queryList() {
        InfoSubcompanyExample infoSubcompanyExample = new InfoSubcompanyExample();
        return infoSubcompanyMapper.selectByExample(infoSubcompanyExample);
    }

    /**
     * @description  获取所有分公司 并且根据用户查询是否选中该分公司增加到返回值中
     * @author  SQL
     * @date    2023/07/06 10:10
     * @param	userid
     * @return  java.util.List<com.zj.fx.model.info.InfoSubcompany>
    */
    public List<InfoSubcompanyVo> getUserisSubCompany(String userid) {
        List<InfoSubcompanyVo> list = new ArrayList<InfoSubcompanyVo>();
        //查询出我的分公司
        TsysUser user = tsysUserMapper.selectByPrimaryKey(userid);
        //查询系统所有的分公司
        List<InfoSubcompany> subcompanies = this.queryList();
        if (StringUtils.isNotEmpty(subcompanies)) {
            for (InfoSubcompany infoSubcompany : subcompanies) {
                Boolean isflag = false;
                InfoSubcompanyVo infoSubcompanyVo = new InfoSubcompanyVo(infoSubcompany.getId(), infoSubcompany.getName(), isflag);
                if (user.getSubcompanyId()!=null){
                    String[] subCompanyIdArr = user.getSubcompanyId().split(",");
                    if (subCompanyIdArr.length > 0) {
                        for (int i = 0; i < subCompanyIdArr.length; i++) {
                            if (String.valueOf(infoSubcompany.getId()).equals(subCompanyIdArr[i])) {
                                isflag = true;
                                break;
                            }
                        }
                    }
                }
                if (isflag) {
                    infoSubcompanyVo.setIscheck(true);
                    list.add(infoSubcompanyVo);
                } else {
                    list.add(infoSubcompanyVo);
                }
            }
        }
        return list;
    }
}
